#include "movieinfo.h"
#include "ui.h"
#include "../cJson/cJSON.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"

extern lv_obj_t *ui_movieImage;
extern lv_obj_t *ui_movieTimeLab;
extern lv_obj_t *ui_moviePriceLab;


static const lv_img_dsc_t *movie_images[] = {
    &ui_img_spiderman_png,
    &ui_img_avengers4_png,
    &ui_img_mov1_png,
    &ui_img_mov2_png,
    &ui_img_mov3_png,
    &ui_img_mov4_png
};


// 轮播控制
lv_timer_t *movie_timer = NULL;
int current_movie_index = 0;
time_t last_user_interact_time = 0;


Movie movie_list[MAX_MOVIES];
int movie_count = 0;

void load_movies_from_json(const char *filename) {
    FILE *fp = fopen(filename, "r");
    if (!fp) {
        printf("无法打开电影信息文件: %s\n", filename);
        return;
    }

    fseek(fp, 0, SEEK_END);
    long filesize = ftell(fp);
    rewind(fp);

    char *data = malloc(filesize + 1);
    if (!data) {
        fclose(fp);
        printf("内存分配失败\n");
        return;
    }

    fread(data, 1, filesize, fp);
    data[filesize] = '\0';
    fclose(fp);

    cJSON *root = cJSON_Parse(data);
    free(data);
    if (!root) {
        printf("JSON解析失败: %s\n", cJSON_GetErrorPtr());
        return;
    }

    cJSON *movies = cJSON_GetObjectItem(root, "movies");
    if (!cJSON_IsArray(movies)) {
        printf("movies 字段不是数组\n");
        cJSON_Delete(root);
        return;
    }

    movie_count = 0;
    cJSON *movie;
    cJSON_ArrayForEach(movie, movies) {
        if (movie_count >= MAX_MOVIES) break;

        cJSON *name = cJSON_GetObjectItem(movie, "name");
        cJSON *time = cJSON_GetObjectItem(movie, "time");
        cJSON *price = cJSON_GetObjectItem(movie, "price");

        if (!cJSON_IsString(name) || !cJSON_IsString(time) || !cJSON_IsNumber(price)) {
            continue;
        }

        strncpy(movie_list[movie_count].name, name->valuestring, sizeof(movie_list[movie_count].name) - 1);
        strncpy(movie_list[movie_count].time, time->valuestring, sizeof(movie_list[movie_count].time) - 1);
        movie_list[movie_count].price = price->valueint;

        // 初始化座位为0
        memset(movie_list[movie_count].seats, 0, sizeof(movie_list[movie_count].seats));

        // 读取座位
        cJSON *seats = cJSON_GetObjectItem(movie, "seats");
        if (cJSON_IsArray(seats)) {
            int r = 0;
            cJSON *row;
            cJSON_ArrayForEach(row, seats) {
                if (r >= ROWS) break;
                if (!cJSON_IsArray(row)) continue;

                int c = 0;
                cJSON *seat;
                cJSON_ArrayForEach(seat, row) {
                    if (c >= COLS) break;
                    movie_list[movie_count].seats[r][c] = seat->valueint;
                    c++;
                }
                r++;
            }
        }

        movie_count++;
    }

    cJSON_Delete(root);
    printf("成功加载 %d 部电影\n", movie_count);
}

void update_movie_display(int index) {
    // printf("movie_count: %d\n", movie_count);
    if (index < 0 || index >= movie_count) {
        printf("电影 index 越界: %d\n", index);
        return;
    }

    Movie *m = &movie_list[index];

    // 设置电影图片
    lv_img_set_src(ui_movieImage, movie_images[index]);

    // 设置电影时间标签
    lv_label_set_text_fmt(ui_movieTimeLab, "TIme: %s", m->time);

    // 设置价格标签
    lv_label_set_text_fmt(ui_moviePriceLab, "Price: %d ", m->price);
}


void save_movies_to_json(const char *filename) {
    cJSON *root = cJSON_CreateObject();
    cJSON *movie_array = cJSON_CreateArray();

    for (int i = 0; i < movie_count; ++i) {
        Movie *m = &movie_list[i];
        cJSON *movie_obj = cJSON_CreateObject();

        cJSON_AddStringToObject(movie_obj, "name", m->name);
        cJSON_AddStringToObject(movie_obj, "time", m->time);
        cJSON_AddNumberToObject(movie_obj, "price", m->price);

        // 添加座位数组
        cJSON *seat_arr = cJSON_CreateArray();
        for (int r = 0; r < ROWS; ++r) {
            cJSON *row_arr = cJSON_CreateArray();
            for (int c = 0; c < COLS; ++c) {
                cJSON_AddItemToArray(row_arr, cJSON_CreateNumber(m->seats[r][c]));
            }
            cJSON_AddItemToArray(seat_arr, row_arr);
        }

        cJSON_AddItemToObject(movie_obj, "seats", seat_arr);
        cJSON_AddItemToArray(movie_array, movie_obj);
    }

    cJSON_AddItemToObject(root, "movies", movie_array);

    char *json_str = cJSON_Print(root);
    FILE *fp = fopen(filename, "w");
    if (fp) {
        fputs(json_str, fp);
        fclose(fp);
    }

    free(json_str);
    cJSON_Delete(root);
}





